package query.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpSession;

import query.bean.Answer;
import query.bean.Paper;
import query.bean.Question;
import query.bean.Questionnaire;
import query.bean.User;
import query.db.PaperRepository;
import query.db.QuestionnaireRepository;
import query.db.UserRepository;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 主页控制类,所有人均可访问
 * 
 * @author lzy
 * @version 1.0.4
 * 
 */
@Controller
@RequestMapping("/")
public class HomeController {

	/**
	 * 用户数据库操作对象
	 */
	@Autowired
	private UserRepository userRepository;

	/**
	 * 问卷数据库操作对象
	 */
	@Autowired
	private QuestionnaireRepository questionnaireRepository;

	/**
	 * 答卷数据库操作对象
	 */
	@Autowired
	private PaperRepository paperRepository;

	/**
	 * 获取网站首页
	 * 
	 * @return 网站首页
	 */
	@RequestMapping(method = GET)
	public String getIndex() {
		return "index";
	}

	/**
	 * 获取登录页面
	 * 
	 * @return 登录页面
	 */
	@RequestMapping(value = "/login", method = GET)
	public String getLogin() {
		return "login";
	}

	/**
	 * 处理用户登录
	 * 
	 * @param userName 用户名
	 * @param password 密码
	 * @return 登录成功与否
	 */
	@RequestMapping(value = "/login", method = POST)
	@ResponseBody
	public String processLogin(String userName, String password, HttpSession session) {
		User user = userRepository.search(userName, password);
		if (user != null) {
			session.setAttribute("user", user);
			if (user.isManager()) {
				session.setAttribute("manager", user.getUserName());
			}
			return "true";
		} else {
			return "false";
		}
	}

	/**
	 * 处理登出
	 * 
	 * @param session 用户session
	 * @return 重定向到网站首页
	 */
	@RequestMapping(value = "/logout", method = GET)
	public String processLogout(HttpSession session) {
		session.invalidate();
		return "redirect:/";
	}

	/**
	 * 获取注册页面
	 * 
	 * @return 注册页面
	 */
	@RequestMapping(value = "/register", method = GET)
	public String getRegister() {
		return "register";
	}

	/**
	 * 处理注册
	 * 
	 * @param userName 用户名
	 * @param password 密码
	 * @param email    电子邮箱
	 * @param session  用户session
	 * @return 注册成功与否
	 */
	@RequestMapping(value = "/register", method = POST)
	@ResponseBody
	public String processRegister(String userName, String password, String email, HttpSession session) {
		if (userRepository.register(userName, password, email)) {
			session.setAttribute("user", new User(userName, password, email));
			return "true";
		} else {
			return "false";
		}
	}

	/**
	 * 获取问卷回答页面
	 * 
	 * @param id      问卷id
	 * @param session 用户session
	 * @return 问卷回答页面
	 */
	@RequestMapping(value = "/{id}", method = GET)
	public String processLink(@PathVariable("id") long id, HttpSession session) {
		List<Question> list = questionnaireRepository.searchQuestions(id);
		Questionnaire questionnaire = questionnaireRepository.searchQuestionnaires(id);
		if (list == null || questionnaire == null) {
			return "redirect:/";
		}
		session.setAttribute("title", questionnaire.getTitle());
		session.setAttribute("questions", list);
		return "link";
	}

	/**
	 * 处理提交答卷
	 * 
	 * @param answer 答卷JSON字符串
	 * @param id     答卷对应问卷的id
	 * @return 操作结果
	 */
	@RequestMapping(value = "/submit/{id}", method = POST)
	@ResponseBody
	public String processSubmit(String answer, @PathVariable("id") long id) {
		Questionnaire questionnaire = questionnaireRepository.searchQuestionnaires(id);
		if (questionnaire.isClose()) {
			return "false";
		}
		Paper paper = paperRepository.insertPaper(id);
		if (paper == null) {
			return "fail";
		}
		List<Answer> list = JSON.parseArray(answer, Answer.class);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setPaperId(paper.getId());
			paperRepository.insertAnswer(list.get(i));
		}
		return "true";
	}
}
